package com.hp.team.bookstore.model.order;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;

import com.hp.team.bookstore.model.catalog.Product;

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class OrderItem {

	/*
	 * Use "GenerationType.TABLE" for table-per-class inheritance strategy.
	 */
	@Id
	@GeneratedValue(strategy = GenerationType.TABLE)
	@Column(name = "ID")
	private long id;

	@OneToOne
	@JoinColumn(name = "BOOK_ID")
	private Product book;

	@Column(name = "QUANTITY")
	private int quantity;

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public Product getBook() {
		return book;
	}

	public void setBook(Product book) {
		this.book = book;
	}

	public int getQuantity() {
		return quantity;
	}

	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof OrderItem) {
			OrderItem otherOrderItem = (OrderItem) obj;

			if (getId() > 0) {
				return this.getId() == otherOrderItem.getId();
			} else {
				return this.getBook().equals(otherOrderItem.getBook());
			}
		}
		return false;
	}

	@Override
	public String toString() {
		return this.book + " : " + this.quantity;
	}

}
